package auctionhaus

import org.springframework.dao.DataIntegrityViolationException

class CustomerController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index() {
		redirect(action: "list", params: params)
	}

	def list() {
		params.max = Math.min(params.max ? params.int('max') : 5, 100)
		[customerInstanceList: Customer.list(params), customerInstanceTotal: Customer.count()]
	}

	def create() {
		[customerInstance: new Customer(params)]
	}

	def save() {
		def customerInstance = new Customer(params)
		if (!customerInstance.save(flush: true)) {
			render(view: "create", model: [customerInstance: customerInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [
			message(code: 'customer.label', default: 'Customer'),
			customerInstance.id
		])
		redirect(action: "show", id: customerInstance.id)
	}

	def show() {
		def customerInstance = Customer.get(params.id)
		if (!customerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[customerInstance: customerInstance]
	}

	def edit() {
		def customerInstance = Customer.get(params.id)
		if (!customerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		[customerInstance: customerInstance]
	}

	def update() {
		def customerInstance = Customer.get(params.id)
		if (!customerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		if (params.version) {
			def version = params.version.toLong()
			if (customerInstance.version > version) {
				customerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[
							message(code: 'customer.label', default: 'Customer')]
						as Object[],
						"Another user has updated this Customer while you were editing")
				render(view: "edit", model: [customerInstance: customerInstance])
				return
			}
		}

		customerInstance.properties = params

		if (!customerInstance.save(flush: true)) {
			render(view: "edit", model: [customerInstance: customerInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [
			message(code: 'customer.label', default: 'Customer'),
			customerInstance.id
		])
		redirect(action: "show", id: customerInstance.id)
	}

	def delete() {
		def customerInstance = Customer.get(params.id)
		if (!customerInstance) {
			flash.message = message(code: 'default.not.found.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
			return
		}

		int bidSize = getCustomerBidSize(customerInstance)
		int listingSize = getCustomerListingSize(customerInstance)

		//C-4: An existing customer can only be deleted through the web interface if they have 0 listings and 0 bids. The system will present an error message to the user if the delete cannot be performed (unit test of the controller action that has this logic)
		if( bidSize > 0 || listingSize > 0 ) {
			if( bidSize > 0 ) {
				flash.message = message(code: 'customer.bids.delete.constraint', args: [
					message(code: 'customer.label', default: 'Customer'),
					params.id
				])
			}
			if( listingSize > 0) {
				flash.message = message(code: 'customer.listings.delete.constraint', args: [
					message(code: 'customer.label', default: 'Customer'),
					params.id
				])
			}
			redirect(action: "show", id: params.id)
			return
		}
		try {
			customerInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [
				message(code: 'customer.label', default: 'Customer'),
				params.id
			])
			redirect(action: "show", id: params.id)
		}
	}
	
	/* Utility methods */
	
	def int getCustomerBidSize(Customer customerInstance)
	{
		int bidSize
		if(customerInstance==null || (customerInstance!=null && customerInstance.bids==null)){
			bidSize = 0;
		}
		else{
			bidSize = customerInstance.bids.size()
		}
		return bidSize
	}
	
	def int getCustomerListingSize(Customer customerInstance)
	{
		int listingSize
		if(customerInstance==null || (customerInstance!=null && customerInstance.listings==null)){
			listingSize = 0;
		}
		else{
			listingSize = customerInstance.listings.size()
		}
		return listingSize
	}
}
